<template>
    <el-dialog width="780px" :title="title" :visible.sync="visible"  >
        <el-form ref="form" :rules="rules" :model="model" label-width="160px"  v-loading="loading">
          <el-form-item prop="contacts" label="联系人">
            <el-input  placeholder="请输入联系人" v-model="model.contacts"></el-input>
          </el-form-item>
          <el-form-item prop="phone" label="联系电话">
            <el-input  placeholder="请输入联系电话" v-model="model.phone"></el-input>
          </el-form-item>
          <el-form-item prop="type" label="处置方式">
            <el-select v-model="model.type" placeholder="请选择处置方式">
              <el-option  :key="1" label="报废" :value="1"> </el-option>
              <el-option  :key="2" label="出售" :value="2"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="disDate" label="处置时间">
            <el-date-picker v-model="model.disDate" type="date"  placeholder="选择日期"> </el-date-picker>
          </el-form-item>
          <el-form-item prop="photo" label="资产处置照片">
            <image-upload v-model="model.disPhoto" ></image-upload>
          </el-form-item>
          <el-form-item prop="note" label="备注">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="model.note">
            </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleCancel">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </div>
    </el-dialog>
</template>

<script>

  import Vue from 'vue'
  import { MessageBox, Message,Notification } from 'element-ui'
  import ImageUpload from "@/components/ImageUpload"
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import { getAction,deleteAction,putAction,postAction } from '@/api/manage'

  export default {
    name: "DisModal",
    components: {ImageUpload},
    data () {
      return {
        title:"处置",
        visible: false,
        loading:false,
        multipleSelection: [],
        model: {},
        url: {

        },  // 新增资产表单验证
        rules: {
          contacts: [{ required: true, message: '请输入联系人', trigger: 'change' }],
          phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
          type: [{ required: true, message: '请选择处置方式', trigger: 'blur' }],
          disDate: [{ required: true, message: '请选择处置时间', trigger: 'blur' }]
        },
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token}

    },
    methods: {
      refresh () {

      },
      add (selection) {
        if(selection.length==0){
          this.$message.warning("请选择需要处置的资产信息！");
          return;
        }
        this.refresh();
        console.log(selection);
        this.edit({assetIds: selection.map( row => row.id )});
      },
      edit (records) {

        let that = this;
        if(that.$refs.form){
          that.$refs.form.resetFields();
        }
        that.model = Object.assign({}, records);
        //that.form.setFieldsValue(pick(this.model,'username','sex','realname','email','phone','activitiSync','workNo','telephone','post'))
        this.visible = true;
      },

      handleSubmit () {
        const that = this;
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true
            let promise;
            promise = postAction("/dispose/add",Object.assign({}, this.model));
            promise.then(res =>{
              if(res.success){
                this.$message.success(res.message)
                that.$emit('ok');
                that.close();
              }else{
                this.$message.warning( res.message)
              }
            }).finally(() => {
              this.loading = false
            });
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      handleCancel () {
        this.close()
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
    }
  }
</script>

<style scoped>

</style>
